#!/bin/bash


# 使用
# 
# sh mysqlImport.sh ./2020-12-22_wordpress_wl.sql
# 
# https://www.cnblogs.com/lonmyblog/p/9235136.html
# http://www.cppcns.com/os/linux/92476.html
# 
# 
# 使用 end 



pwdPath=${PWD}/variable-group
# echo ${pwdPath}
# echo ""

# =============== 加载其他文件的内容 ==================

# variableMysql="${PWD}/variable-mysql.sh"
# echo ${variableMysql}
# echo ""

# variableFile="${PWD}/variable-file.sh"
# echo ${variableFile}
# echo ""

# params="${pwdPath}/params.sh"
# echo ${params}

# source ${params}
# source ${variableFile}
# source ${variableMysql}


sourceFile="${pwdPath}/source-file.sh"
echo ${sourceFile}
source ${sourceFile}

# =============== 加载其他文件的内容 ==end=============

echo ${HOSTNAME}
# echo ""

echo ${PORT}
# echo ""

echo ${USERNAME}
# echo ""

echo ${PASSWORD}
# echo ""

echo ${DBNAME}
# echo ""

echo ${PathFile}
# echo ""

# DBNAME='dddddddddf'


echo ${muidUs}
echo ${mids}
echo "${dates}-${fileName}"

echo ${dates}

pathFileSaveSql=${muidUs}


echo ${pathFileSaveSql}
echo ""

if [ -z "$pathFileSaveSql" ]; then
	echo "pathFileSaveSql is empty"
	exit 10
fi

if [ -z "$muidUs" ]; then
	echo "muid is empty"
	exit 10
fi

# 这里的-f参数判断$pathFileSaveSql 文件是否存在
if [ ! -f "$pathFileSaveSql" ]; then
	echo "pathFileSaveSql is empty"
	exit 10
fi

# 这里的-d参数判断$pathFileSaveSql 文件夹[目录]是否存在
if [ ! -d "$pathFileSaveSql" ]; then
	echo "pathFileSaveSql is empty"
	# exit 10
fi

select_sql="show databases "

dbNameExit=`${MYSQLBINSH}  -h${HOSTNAME}  -P${PORT} -u${USERNAME} -p${PASSWORD}  -e "${select_sql}" | grep "${DBNAME}"`

echo  "${dbNameExit}"

if [ -z "$dbNameExit" ]; then
	echo "dbNameExit is empty ,数据库不存在正在创建数据库中 ${DBNAME} "
	createDbName="create database ${DBNAME} ; "
	${MYSQLBINSH}  -h${HOSTNAME}  -P${PORT} -u${USERNAME} -p${PASSWORD}  -e "${createDbName}"
	echo "数据库创建 ${DBNAME} 创建成功"

fi

echo "数据库 ${DBNAME} 正在导入中"

mysqlImport="source ${pathFileSaveSql}"

data=`${MYSQLBINSH}  -h${HOSTNAME}  -P${PORT} -u${USERNAME} -p${PASSWORD} -D ${DBNAME} -e "${mysqlImport}" `

echo  "${data}"

echo "数据库创建 ${DBNAME} 导入成功"



